package staffManager;

import Bean.CheckBean;
import Bean.SqlBean;
import loginRegister.LoginBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author 黄忠
 */
public class UpdateStaffServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String userName=new String(request.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
        String edu=new String(request.getParameter("edu").getBytes("ISO-8859-1"),"UTF-8");
        String work=new String(request.getParameter("work").getBytes("ISO-8859-1"),"UTF-8");
        String monthPay=new String(request.getParameter("monthPay").getBytes("ISO-8859-1"),"UTF-8");
        String level=new String(request.getParameter("level").getBytes("ISO-8859-1"),"UTF-8");

        HttpSession session=request.getSession();
        LoginBean login=(LoginBean)session.getAttribute("login");
        if(login==null){
            response.sendRedirect("http://localhost:8080/login.jsp");
        }
        else if(monthPay.contains("-")){
            CheckBean.alert("工资不能为负数!");
            response.sendRedirect("http://localhost:8080/staffManager/updateStaff.jsp");
        }
        else {
            String sql = "select * from user where userName='" + userName + "'";
            try {
                ResultSet rs = null;
                SqlBean sb = new SqlBean();
                sb.connectSQL();
                rs = sb.executeQuery(sql);
                rs.last();
                if (rs.getRow()>0){
                    sql="update user set edu ='"+edu+"',work='"+work+"',monthPay='"+monthPay+"',level='"+level+"' where username='"+userName+"'";
                    sb.executeUpdate(sql);
                    sql = "select * from user";
                    rs = sb.executeQuery(sql);
                    ArrayList staffslist = null;
                    staffslist = new ArrayList();
                    while (rs.next()) {
                        if (!login.getUserName().equals(rs.getString("userName"))) {
                            StaffBean ff = new StaffBean(rs);
                            if(!ff.getLevel().equals("超级管理员"))
                                staffslist.add(ff);
                        }
                    }
                    session.setAttribute("staffslist", staffslist);
                    sb.closeSQL();
                    CheckBean.alert("修改成功!");
                    response.sendRedirect("http://localhost:8080/staffManager/updateStaff.jsp");
                }
                else {
                    CheckBean.alert("用户不存在!");
                    response.sendRedirect("http://localhost:8080/staffManager/updateStaff.jsp");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        doGet(request, response);
    }
}
